package com.example.springboot.controller;

import com.example.springboot.pojo.PageBean;
import com.example.springboot.pojo.Result;
import com.example.springboot.pojo.User;
import com.example.springboot.service.UserService;
import com.example.springboot.util.Encrypt;
import com.example.springboot.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/findByRoomNumber")
    public User findByRoomNumber(String roomNumber){
        return userService.findByRoomNumber(roomNumber);
    }

//    @GetMapping("/userInfo")
//    public Result<?> adminInfo(@RequestHeader(name = "Authorization") String token){
//        Map<String, Object> map = JwtUtil.parseToken(token);
//        return Result.success(map);
//    }

    @GetMapping("/getAllUser")
    public Result<PageBean<User>> getAllUser(Integer pageNum,Integer pageSize){
//        System.out.println(pageNum);
//        System.out.println(pageSize);
        PageBean<User> pb = userService.getAllUser(pageNum,pageSize);
        return Result.success(pb);
    }

    @PostMapping("/addUser")
    public Result<?> addUser(String roomNumber,String name,String id,String phoneNumber,String email){
        //查询用户是否存在
//        System.out.println(roomNumber);
        User u = userService.findByRoomNumber(roomNumber);
        if(u == null){
            userService.addUser(roomNumber,name,id,phoneNumber,id.substring(id.length() - 6),email);
            return Result.success();
        }else{
            return Result.error("此业主已经存在");
        }
    }

    @PutMapping("/updateUser")
    public Result<?> updateUser(String roomNumber,String name,String id,String phoneNumber,String email){
        User u = userService.findByRoomNumber(roomNumber);
        if(u != null){
            userService.updateUser(roomNumber,name,id,phoneNumber,email);
            return Result.success();
        }else{
            return Result.error("此业主不存在");
        }
    }

    @PutMapping("/deleteUser")
    public Result<?> deleteUser(String roomNumber){
//        System.out.println("-----------------" + roomNumber);
        User u = userService.findByRoomNumber(roomNumber);
        if(u != null){
            userService.deleteUser(roomNumber);
            return Result.success();
        }else{
            return Result.error("此业主不存在");
        }
    }

    @PutMapping("/login")
    public Result<?> login(String roomNumber,String password){
//        System.out.println(roomNumber);
        User u = findByRoomNumber(roomNumber);
        if(u != null){
            String encryptPsd = Encrypt.toMD5(password);
            if(u.getPassword().equals(encryptPsd)){
                // 登录成功
                Map<String,Object> claims = new HashMap<>();
                claims.put("userId",roomNumber);
                claims.put("name",u.getName());
                claims.put("phoneNumber",u.getPhoneNumber());
                claims.put("id",u.getId());
                claims.put("email",u.getEmail());
                String token = JwtUtil.genToken(claims);
                return Result.success(token);
            }else{
                return Result.error("密码错误");
            }
        }else{
            return Result.error("不存在的账号");
        }
    }
}
